<?php

require_once 'Zend/Application.php';
require_once 'St/Controller/Action/Admin.php';

Class St_Application extends Zend_Application {

    public function __construct($environment, $options = null) {
        parent::__construct($environment, $options);
        $this->init();
    }

    public function init() {
        St_Registry::set("config", $this->_options);
        $this->initDb();
        $this->initLayout();
        $this->initMultiLanguage();

        // turn on debug mode
        ini_set('display_startup_errors', 1);
        ini_set('display_errors', 1);
    }

    public function initDb() {
        $db = Zend_Db::factory($this->_options['database']['adapter'], $this->_options['database']['params']);
        St_Registry::set('db', $db);
        include_once 'Zend/Db/Table/Abstract.php';
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
    }

    public function initLayout() {
        Zend_Loader::loadClass("Zend_Layout");
        $this->_options['layout']['layoutPath'] .= St_Registry::getConfig("template") . "/";
        Zend_Layout::startMvc($this->_options['layout']);
    }

    public function initMultiLanguage() {
        Zend_Loader::loadClass("Zend_Translate");
        //init multilanguage
        $languagePath = "languages/";
        $handle = @opendir($languagePath);
        if (!$handle) {
            $languagePath = "../languages/";
            $handle = @opendir($languagePath);
        }
        $translate = null;

        if ($handle) {
            while (false !== ($file = readdir($handle))) {
                $information = pathinfo($file);
                if ($information['extension'] == "php") {
                    if (!$translate) {
                        $translate = new Zend_Translate('Array', $languagePath . $information['basename'], $information['filename']);
                    } else {
                        $translate->addTranslation($languagePath . $information['basename'], $information['filename']);
                    }
                }
            }
            closedir($handle);
        } else {
            die("can't read language file");
        }
        St_Registry::set("translator", $translate);
    }

}